package com.jinke.uang.ui.activity.validation;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.text.format.Time;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jinke.uang.R;
import com.jinke.uang.base.BaseNormalActivity;
import com.jinke.uang.base.BaseResponse;
import com.jinke.uang.common.RouterMap;
import com.jinke.uang.common.NoDoubleClickListener;
import com.jinke.uang.common.UserAuthRouter;
import com.jinke.uang.common.UserInfo;
import com.jinke.uang.constant.Constant;
import com.jinke.uang.constant.HttpConstant;
import com.jinke.uang.dao.LocationDao;
import com.jinke.uang.bean.AuthUserInfoData;
import com.jinke.uang.model.AuthModule;
import com.jinke.uang.presenter.CommonPresenter;
import com.jinke.uang.bean.DictionaryItemData;
import com.jinke.uang.utils.ToastUtils;
import com.jinke.uang.view.LocationDialog;
import com.jinke.uang.view.MenuDialog;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.qqtheme.framework.picker.DatePicker;
import cn.qqtheme.framework.picker.DateTimePicker;

/**
 * 单位认证
 */
@Route(path = RouterMap.WORK_AUTH_PAGE)
public class CompanyAuthActivity extends BaseNormalActivity<CommonPresenter,AuthModule>{
    @BindView(R.id.topBack)
    ImageView topBack;
    @BindView(R.id.topText)
    TextView topText;
    @BindView(R.id.etCompanyName)
    EditText etCompanyName;
    @BindView(R.id.etWorkType)
    TextView etWorkType;
    @BindView(R.id.rlWorkType)
    RelativeLayout rlWorkType;
    @BindView(R.id.etJob)
    TextView etJob;
    @BindView(R.id.rlJob)
    RelativeLayout rlJob;
    @BindView(R.id.etWorkTime)
    TextView etWorkTime;
    @BindView(R.id.rlWorkTime)
    RelativeLayout rlWorkTime;
    @BindView(R.id.etMoney)
    TextView etMoney;
    @BindView(R.id.rlMoney)
    RelativeLayout rlMoney;
    @BindView(R.id.etCall)
    EditText etCall;
    @BindView(R.id.etWeb)
    EditText etWeb;
    @BindView(R.id.etProvince)
    TextView etProvince;
    @BindView(R.id.etCity)
    TextView etCity;
    @BindView(R.id.etTown)
    TextView etTown;
    @BindView(R.id.etArea)
    TextView etArea;
    @BindView(R.id.etAddress)
    EditText etAddress;
    @BindView(R.id.etPost)
    EditText etPost;
    @BindView(R.id.tvNext)
    TextView tvNext;
    @Autowired(name = Constant.IS_BACK)
    boolean is_back;

    private MenuDialog mMenuDialog;
    private LocationDialog mLocationDialog;
    private ArrayList<DictionaryItemData> mWorkTypeList;
    private ArrayList<DictionaryItemData> mSalaryList;
    private ArrayList<DictionaryItemData> mJobLevelList;
    // 工作类型
    private final String DICTIONARY_TYPE_WORK_TYPE = "common_professional";
    private final String WORK_TYPE = "1";
    // 工资
    private final String DICTIONARY_TYPE_SALARY_TREATMENT = "salary_treatment";
    private final String SALARY_TREATMENT = "2";
    // 目前职位
    private final String DICTIONARY_TYPE_JOB_LEVEL = "current_position";
    private final String JOB_LEVEL = "3";
    private ArrayList<String> mProvinces;
    private ArrayList<String> mCitys;
    private ArrayList<String> mTowns;
    private ArrayList<String> mAreas;
    private LocationDao dao;
    public static String REASONCODE = "orderBackReasonCode";
    private final String COMPANY_CODE ="company_code";
    private String reasonCode;


    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        topBack.setOnClickListener(viewclick);
        //获取头部信息
        topText.setText(getStringResource(R.string.auth_company_title));
        reasonCode = getIntent().getStringExtra(REASONCODE);
        if (!TextUtils.isEmpty(reasonCode)) {
            tvNext.setText(R.string.save_change);
        }
        rlWorkType.setOnClickListener(viewclick);
        etWorkType.setOnClickListener(viewclick);
        rlJob.setOnClickListener(viewclick);
        etJob.setOnClickListener(viewclick);

        rlWorkTime.setOnClickListener(viewclick);
        etWorkTime.setOnClickListener(viewclick);
        rlMoney.setOnClickListener(viewclick);
        etMoney.setOnClickListener(viewclick);

        //地址信息
        etProvince.setOnClickListener(viewclick);
        etCity.setOnClickListener(viewclick);
        etTown.setOnClickListener(viewclick);

        etArea.setOnClickListener(viewclick);
        tvNext.setOnClickListener(viewclick);
    }


    @Override
    protected void onUIReady() {
        ARouter.getInstance().inject(this);
        if (UserInfo.isShowUserInformation()) {
            mPresenter.getData(HttpConstant.AUTH_INFO_DATA,null,COMPANY_CODE);
        }
        if (is_back) {
            tvNext.setText(R.string.save_change);
        }

        mPresenter.getData(HttpConstant.DICTIONARY_DATA,WORK_TYPE,DICTIONARY_TYPE_WORK_TYPE);
        mPresenter.getData(HttpConstant.DICTIONARY_DATA,JOB_LEVEL,DICTIONARY_TYPE_JOB_LEVEL);
        mPresenter.getData(HttpConstant.DICTIONARY_DATA,SALARY_TREATMENT,DICTIONARY_TYPE_SALARY_TREATMENT);

        dao = new LocationDao();
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_auth_company;
    }

    @Override
    public String getPageTitle() {
        return "";
    }

    @Override
    protected int getResId() {
        return 0;
    }

    @Override
    protected CommonPresenter getPresenter() {
        return new CommonPresenter();
    }

    @Override
    protected AuthModule getModule() {
        return new AuthModule();
    }

    /**
     * 点击信息
     */
    private NoDoubleClickListener viewclick = new NoDoubleClickListener() {
        @Override
        public void onNoDoubleClick(View v) {
            super.onNoDoubleClick(v);
            switch (v.getId()) {
                case R.id.topBack:
                    finish();
                    break;
                case R.id.rlWorkType:
                case R.id.etWorkType:
                    //获取工作类型
                    picker_work_type();
                    break;
                case R.id.rlWorkTime:
                case R.id.etWorkTime:
                    //获取工作时间
                    pickerTime();
                    break;
                case R.id.rlMoney:
                case R.id.etMoney:
                    //获取薪酬
                    pickSalary();
                    break;
                case R.id.rlJob:
                case R.id.etJob:
                    //工作
                    picker_job_level();
                    break;
                case R.id.etProvince:
                    //获取地址字典
                    pickerProvince();
                    break;
                case R.id.etCity:
                    pickerCity();
                    break;
                case R.id.etTown:
                    pickerTown();
                    break;
                case R.id.etArea:
                    pickerArea();
                    break;
                case R.id.tvNext:
                    commit();
                    break;
                default:
                    break;
            }
        }
    };

    /**
     * 获取工作类型
     */
    private void picker_work_type() {
        if (mWorkTypeList == null || mWorkTypeList.size() == 0) {
            return;
        }
        getMenuDialog().showDialog(etWorkType, mWorkTypeList);
    }

    /**
     * 工作的
     */
    private void picker_job_level() {
        if (mJobLevelList == null || mJobLevelList.size() == 0) {
            return;
        }
        getMenuDialog().showDialog(etJob, mJobLevelList);
    }

    /**
     * 薪资水平
     */
    private void pickSalary() {
        if (mSalaryList == null || mSalaryList.size() == 0) {
            return;
        }
        getMenuDialog().showDialog(etMoney, mSalaryList);
    }

    /**
     * 工作时间选择
     */
    private DatePicker picker;

    @SuppressLint("SetTextI18n")
    private void pickerTime() {
        //24小时值
        Time time = new Time();
        time.setToNow();
        int year = time.year;
        int month = time.month + 1;

        if (picker == null) {

            picker = new DatePicker(this, DateTimePicker.YEAR_MONTH);
            picker.setLabel(getStringResource(R.string.year_unit), getStringResource(R.string
                    .month_unit), getStringResource(R.string.day_unit));
            //日期起点
            picker.setRangeStart(year - 30, 1);
            //日期终点
            picker.setRangeEnd(year, month);
            // 设置默认选中值
            picker.setSelectedItem(year, 1);

            picker.setOnDatePickListener(new DatePicker.OnYearMonthPickListener() {
                @Override
                public void onDatePicked(String year, String month) {
                    etWorkTime.setText(year + "-" + month);
                }
            });
        }
        picker.show();
    }

    /**
     * 获取省信息
     */
    private void pickerProvince() {
        mProvinces = dao.getProvince();
        getLocationDialog().showDialog(etProvince, mProvinces);
    }

    /**
     * 获取市信息
     */
    private void pickerCity() {
        String province = etProvince.getText().toString();
        mCitys = dao.getCity(province);
        getLocationDialog().showDialog(etCity, mCitys);
    }

    /**
     * 获取镇信息
     */
    private void pickerTown() {
        String province = etProvince.getText().toString();
        String city = etCity.getText().toString();
        mTowns = dao.getTown(province, city);
        getLocationDialog().showDialog(etTown, mTowns);
    }

    private void pickerArea() {
        String province = etProvince.getText().toString();
        String city = etCity.getText().toString();
        String town = etTown.getText().toString();
        mAreas = dao.getArea(province, city, town);
        getLocationDialog().showDialog(etArea, mAreas);
    }


    /**
     * 根据查询结果展示相关的信息
     *
     * @param companyInfo
     */
    private void fillTextView(AuthUserInfoData.CompanyInfoBean companyInfo) {

        etProvince.setText(companyInfo.getCompanyProvince());
        etCity.setText(companyInfo.getCompanyCity());
        etTown.setText(companyInfo.getCompanyTown());
        etArea.setText(companyInfo.getCompanyRegion());
        etAddress.setText(companyInfo.getCompanyAddressDesc());
        etPost.setText(companyInfo.getCompanyZipcode());

        etCompanyName.setText(companyInfo.getCompanyName());
        etWeb.setText(companyInfo.getCompanyNet());
        etCall.setText(companyInfo.getCompanyTel());
        etWorkTime.setText(companyInfo.getWorkStartTime());

        etJob.setText(companyInfo.getCommonProfessionalValue());
        etJob.setTag(companyInfo.getCommonProfessional());

        etWorkType.setText(companyInfo.getCompanyIndustryValue());
        etWorkType.setTag(companyInfo.getCompanyIndustryCode());
        etMoney.setText(companyInfo.getSalaryValue());
        etMoney.setTag(companyInfo.getSalary());

        etProvince.setText(companyInfo.getCompanyProvince());
        etCity.setText(companyInfo.getCompanyCity());
        etTown.setText(companyInfo.getCompanyTown());
        etArea.setText(companyInfo.getCompanyRegion());
        etAddress.setText(companyInfo.getCompanyAddressDesc());
        etPost.setText(companyInfo.getCompanyZipcode());
    }

    private void commit() {
        String companyName = etCompanyName.getText().toString().trim();
        String workType = etWorkType.getText().toString().trim();
        String workTypeCode = (String) etWorkType.getTag();
        String jobLevel = etJob.getText().toString().trim();
        String jobLevelCode = (String) etJob.getTag();
        String workAge = etWorkTime.getText().toString().trim();
        String salary = etMoney.getText().toString().trim();
        String salaryCode = (String) etMoney.getTag();
        String companyTel = etCall.getText().toString().trim();
        String companyNet = etWeb.getText().toString().trim();
        String province = etProvince.getText().toString().trim();
        String city = etCity.getText().toString().trim();
        String town = etTown.getText().toString().trim();
        String area = etArea.getText().toString().trim();
        String postNo = etPost.getText().toString().trim();
        String address = etAddress.getText().toString().trim();
        if (TextUtils.isEmpty(companyName)) {
            ToastUtils.showShort(this, R.string.work_auth_company_name_hint);
            return;
        } else if (TextUtils.isEmpty(workType) || TextUtils.isEmpty(workTypeCode)) {
            ToastUtils.showShort(this, R.string.work_auth_work_type_hint);
            return;
        } else if (TextUtils.isEmpty(jobLevel) || TextUtils.isEmpty(jobLevelCode)) {
            ToastUtils.showShort(this, R.string.work_auth_job_level_hint);
            return;
        } else if (TextUtils.isEmpty(workAge)) {
            ToastUtils.showShort(this, R.string.work_auth_work_age_hint);
            return;
        } else if (TextUtils.isEmpty(salary) || TextUtils.isEmpty(salaryCode)) {
            ToastUtils.showShort(this, R.string.work_auth_salary_hint);
            return;
        } else if (TextUtils.isEmpty(companyTel)) {
            ToastUtils.showShort(this, R.string.work_auth_company_tel_hint);
            return;
        } else if (TextUtils.isEmpty(companyNet)) {
            ToastUtils.showShort(this, R.string.work_auth_company_net_hint);
            return;
        } else if (TextUtils.isEmpty(province)) {
            ToastUtils.showShort(this, R.string.province_hint);
            return;
        } else if (TextUtils.isEmpty(city)) {
            ToastUtils.showShort(this, R.string.work_auth_city_hint);
            return;
        } else if (TextUtils.isEmpty(town)) {
            ToastUtils.showShort(this, R.string.work_auth_company_town_hint);
            return;
        } else if (TextUtils.isEmpty(area)) {
            ToastUtils.showShort(this, R.string.work_auth_company_area_hint);
            return;
        } else if (TextUtils.isEmpty(address)) {
            ToastUtils.showShort(this, R.string.auth_type_company_address_hint);
            return;
        } else if (TextUtils.isEmpty(postNo)) {
            ToastUtils.showShort(this, R.string.work_auth_company_post_hint);
            return;
        }

        mPresenter.getData(HttpConstant.SAVE_PERSONAL_AUTH,null,companyName,workTypeCode,jobLevelCode,
                workAge,salaryCode, companyTel,companyNet,province,city,town,area,address,postNo,COMPANY_CODE);
        showLoadingView();
    }

    public MenuDialog getMenuDialog() {
        if (mMenuDialog == null) {
            mMenuDialog = new MenuDialog(this, R.style.dialog_bottom_full);
        }
        return mMenuDialog;
    }

    public LocationDialog getLocationDialog() {
        if (mLocationDialog == null) {
            mLocationDialog = new LocationDialog(this, R.style.dialog_bottom_full);
        }

        return mLocationDialog;
    }


    @Override
    public void showResponse(int apiType, String listType,Object o) {
        dismissLoadingView();
        if(apiType== HttpConstant.AUTH_INFO_DATA){ //获取单位认证信息
            dismissLoadingView();
            BaseResponse<AuthUserInfoData> response = (BaseResponse<AuthUserInfoData>) o;
            fillTextView(response.getData().getCompanyInfo());
        }

        else if(apiType==HttpConstant.SAVE_PERSONAL_AUTH){ //保存单位认证信息接口
            dismissLoadingView();
            BaseResponse<String> response = (BaseResponse<String>) o;
            if (response.getStatus() == Constant.HTTP_OK) {
                if (!is_back) {
                    UserAuthRouter.getInstance().jumpNextPager(this, RouterMap.WORK_AUTH_PAGE);
                } else {
                    finish();
                }
            } else {
                ToastUtils.showShort(this, response.getMessage());
            }
        }

        else if(apiType==HttpConstant.DICTIONARY_DATA){ //获取字典信息
            dismissLoadingView();
            if(listType.equals(WORK_TYPE)){
                BaseResponse<ArrayList<DictionaryItemData>> listBaseResponse = (BaseResponse<ArrayList<DictionaryItemData>>) o;
                mWorkTypeList = listBaseResponse.getData();
            }
            else if(listType.equals(JOB_LEVEL)){
                BaseResponse<ArrayList<DictionaryItemData>> listBaseResponse = (BaseResponse<ArrayList<DictionaryItemData>>) o;
                mJobLevelList=listBaseResponse.getData();
            }
            else if(listType.equals(SALARY_TREATMENT)){
                BaseResponse<ArrayList<DictionaryItemData>> listBaseResponse = (BaseResponse<ArrayList<DictionaryItemData>>) o;
                mSalaryList=listBaseResponse.getData();
            }
        }
    }

    @Override
    public void showError(Throwable e) {
        dismissLoadingView();
        ToastUtils.showShort(this, R.string.network_error);
    }
}
